<?php


namespace app\api\model;


use app\common\model\BaseModel;
use app\api\model\RoleModel;

class ACLModel extends BaseModel
{
    protected $name = 'acl_uri';
    protected $pk = 'id';


    function get_role_uri_ids($city_id, $role_id, $type)
    {
        $rows = null;
        if ($role_id == STAFF_ROLE_SUPER_ADMIN){
            $rows = $this->where('type',$type)->distinct(true)->field('id')->select();
        }else{
            $rows = $this->where('id','IN',function ($query) use ($city_id,$role_id,$type){
                $where =[
                    'role_id'=>$role_id,
                    'type' => $type,
                    'is_deleted' =>0
                ];
                $query->table('tb_role_acl')->where($where)->where('city_id', ['=', null], ['=', $city_id], 'or')->distinct(true)->field('uri_id');
            })->distinct(true)->field('id')->select();
        }

        $l =[];
        foreach ($rows as $row){
            $l[] = $row->getAttr('id');
        }

        $l = array_unique($l);

        return $l;
    }


    function get_role_uri_tree($city_id, $role_id, $type){

        $level = (new RoleModel())->role_level($role_id);

        $rows = null;

        if ($level <= STAFF_ROLE_LEVEL_RESERVED_MAX) {
            $st = "select DISTINCT  uri from tb_acl_uri where type = ? and level >= ?";
            $beans = [
                'type' =>$type,
                'level' => ['>=',$level]
            ];
            $rows = $this->where($beans)->distinct(true)->field('uri')->select();
        } else {
            $rows = $this->where('id','IN',function ($query) use($type,$city_id,$role_id){
                $query->where(['type' => $type,'role_id'=>$role_id])->where('city_id',['=',null],['=',$city_id],'or');
            })->distinct(true)->field('uri')->select();
        }
        $uris = [];
        foreach ($rows as $row) {
            $uris[] = $row->getAttr('uri');
        }

        $list = $this->query_list($type);
        $root = [
            'id' => 0,
            'uri' => null,
            'visitable' => 1,
            'children' => [],
        ];

        \common::list_to_tree($list, $root);

        $this->check_visitable_recursion($root, $uris);

        return [$uris, $root];
    }

    function check_visitable_recursion_branch(&$root) {
        $useful = false;
        if ($root['visitable']) {
            $useful = true;
        }

        for ($i = 0; $i < count($root['children']);) {
            $ret = $this->check_visitable_recursion_branch($root['children'][$i]);
            if (!$ret) {
                array_splice($root['children'], $i, 1);
            } else {
                $i++;
            }
        }
        if (!$useful && count($root['children']) > 0) {
            $useful = true;
        }
        return $useful;
    }

    function check_visitable_recursion(&$root, $uris) {
        if ($root['id'] == 0 || in_array($root['uri'], $uris)) {
            $root['visitable'] = 1;
        } else {
            $root['visitable'] = 0;
        }
        foreach ($root['children'] as &$child) {
            $this->check_visitable_recursion($child, $uris);
        }
    }

    function get_user_uris_baseon($city_id, $user_id, $type) {
    }

    function query_list($type, $level = -1) {
        $selectlist = [
            'id' => 'int',
            'name' => 'string',
            'parent' => 'string',
            'uri' => 'string',
            'uri_type' => 'string',
            'baseon' => 'string',
        ];

        $beans = [
            'type' => $type,
            'is_deleted' => 0,
        ];

        if ($level > 0) {
            $beans['level'] = ['>',$level];
        }

        $rows = $this->where($beans)->select();

        $l = [];
        foreach ($rows as $row) {
            $info = $row->toArray();
            $baseon = [];
            if (!empty($info['baseon'])) {
                $baseon = explode(',', $info['baseon']);
            }
            $info['baseon'] = $baseon;
            $info['title'] = $info['name'];
            $l[] = $info;
        }
        return $l;
    }

    function reset_uris($version, $type, $data) {
        $beans = [
            'type' => $type,
            'version' => ['<>',$version],
            'is_deleted' => 0
        ];

        self::update([
            'is_deleted' => 1
        ],$beans);


        foreach ($data as $arr) {
            $info = [
                'type' => $type,
                'id' => $arr['id'],
                'name' => $arr['name'],
                'parent' => $arr['parent'],
                'uri' => $arr['uri'],
                'uri_type' => $arr['uri_type'],
                'baseon' => implode(",", $arr['baseon']),
                'remark' => $arr['remark'],
                'is_deleted' => 0,
                'version' => $version,
            ];
            self::create($info,true);
        }
    }
}